<template>
	<section>

		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="seach">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="dictionarydetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="字典明细" width="360" sortable>
			</el-table-column>
			<el-table-column prop="dicType.id" label="字典类型" width="360" :formatter="formatSex" sortable>
				<template scope="scope">
					<span size="small" style="color: skyblue;" v-if="scope.row.dicType.id === 1">产品品牌</span>
					<span size="small" style="color: pink;" v-else-if="scope.row.dicType.id === 2">产品单位</span>
					<span size="small" style="color: gray" v-else>其它</span>
				</template>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :current-page="currentPage"
						   :total="total" style="float:right;">
			</el-pagination>
		</el-col>


		<!--编辑和新增界面-->
		<el-dialog :title="title" :visible.sync="dictionarydetailFormVisible" :close-on-click-modal="false">
			<el-form :model="dictionarydetailForm" label-width="80px" :rules="dictionarydetailFormRules" ref="dictionarydetailForm">
				<el-form-item label="字典名称" prop="name">
					<el-input v-model="dictionarydetailForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="字典类型">
					<el-select v-model="dictionarydetailForm.dicType" clearable value-key="id" placeholder="请选择">
						<!--
                             value-key:作为 value 唯一标识的键名，绑定值为对象类型时必填
                             value/v-model：绑定值，要传给后端的值
                             label="item.username"：要显示的值
                             clearable：可情况的
                         -->
						<el-option
								v-for="item in dictionarytype"
								:key="item.id"
								:label="item.name"
								:value="item">
							<span style="float: left">{{ item.name }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="dictionarydetailFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="dictionarydetailSubmit" :loading="dictionarydetailLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
    //import NProgress from 'nprogress'
    import {
        addDictionaryDetail,
        batchRemoveDictionaryDetail,
        dictionarydetailDictionaryDetail,
        getDictionaryDetailListPage,
        removeDictionaryDetail
    } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					keyword: ''
				},
				dictionarydetails: [],
				dictionarytype: [],
                title:"",
				total: 0,
                currentPage: 1,
				pageSize:5,
				listLoading: false,
				sels: [],//列表选中列

				dictionarydetailFormVisible: false,//编辑界面是否显示
                table: false,
				dictionarydetailLoading: false,
				dictionarydetailFormRules: {
					name: [
						{ required: true, message: '请输入产品名称', trigger: 'blur' }
					]
				},
				//编辑界面数据
				dictionarydetailForm: {
                    id: null,
                    name: "",
                    dicType: null
				}

			}
		},
		methods: {
			//状态显示转换
			formatSex: function (row, column) {
				return row.dicType.id == 1 ? '产品品牌' : row.dicType.id == 2 ? '产品单位' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictionaryDetails();
			},
			// 高级查询页码定位到第一页
            seach() {
			    this.currentPage = 1;
			    this.getDictionaryDetails();
			},
			//获取数据字典明细列表
			getDictionaryDetails() {
				let para = {
                    currentPage:this.currentPage,
                    keyword: this.filters.keyword,
					pageSize:this.pageSize
				};
				this.listLoading = true;
				this.$http.post("/detail",para)
					.then(result => {
					    this.dictionarydetails = result.data.rows;
					    // 关闭盲等
                        this.listLoading = false;
                        this.total = result.data.total;
                    })
					.catch(result => {
					    console.log();
					    alert("系统错误!");
					})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {// 点击确认
					this.listLoading = true;// 开启忙等
					//NProgress.start();
					// let para = { id: row.id };
					this.$http.delete("/detail/" + row.id)
						.then(result => {
						    // 关闭盲等
                            this.listLoading = false;
                            // 返回AjaxResult对象
                            result = result.data;
                            if (result.success) {// 如果success为true
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
							}else {
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
							}
							// 删除之后需要回到第一页（看业务需求）
							this.currentPage = 1;
                            // 重新刷新页面
							this.getDictionaryDetails();
						})
						.catch(result => {
                            // 关闭盲等
                            this.listLoading = false;
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            // 失败了也要回到第一页（还是看需求）
                            this.currentPage = 1;
                            // 依然要刷新列表数据
                            this.getDictionaryDetails();
						})
				}).catch(() =>{// 点击取消

				})
			},
			//显示编辑界面
			handleEdit: function (index, row) {
			    this.title = "编辑";
				this.dictionarydetailFormVisible = true;
				// 克隆当前行数据做回显数据
				this.dictionarydetailForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
                this.title = "新增";
				this.dictionarydetailFormVisible = true;
				this.dictionarydetailForm = {// 情况遗留数据
					id:null,
					name: '',
					dicType: null
				};
			},
			// 表单数据提交
			dictionarydetailSubmit: function () {
				this.$refs.dictionarydetailForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.dictionarydetailLoading = true;
							// 点击提交的时候关闭操作对话框
                            this.dictionarydetailFormVisible = false;
							//NProgress.start();
							// 克隆表单数据
							let para = Object.assign({}, this.dictionarydetailForm);
							this.$http.put("/detail",para)
								.then(result => {
                                    this.dictionarydetailLoading = false;
								    // 返回AjaxResult对象
                                    result = result.data;
									if (result.success) {// 如果success为true
										this.$message({
											message: '操作成功',
											type: 'success'
										});
									}else {
										this.$message({
											message: result.message,
											type: 'error'
										});
									}
									// 删除之后需要回到第一页（看业务需求）
									this.currentPage = 1;
									// 重新刷新页面
									this.getDictionaryDetails();
								})
								.catch(result => {
                                    // 关闭盲等
                                    this.listLoading = false;
                                    this.$message({
                                        message: '操作失败',
                                        type: 'error'
                                    });
                                    // 失败了也要回到第一页（还是看需求）
                                    this.currentPage = 1;
                                    // 依然要刷新列表数据
                                    this.getDictionaryDetails();
								})
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					// let para = { ids: ids };
					this.$http.patch("/detail", ids)
						.then(result => {
						    // 关闭忙等框
                            this.listLoading = false;
                            // 返回AjaxResult对象
                            result = result.data;
                            if (result.success) {// 如果success为true
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else {
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                            // 删除之后需要回到第一页（看业务需求）
                            this.currentPage = 1;
                            // 重新刷新页面
                            this.getDictionaryDetails();
						})
						.catch(result => {
                            // 关闭盲等
                            this.listLoading = false;
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            // 失败了也要回到第一页（还是看需求）
                            this.currentPage = 1;
                            // 依然要刷新列表数据
                            this.getDictionaryDetails();
						})
				}).catch(() => {

				});
			},
			// 获数据字典类型数据
             getDictionaryType() {
			    this.$http.get("/type")
					.then(result => {
					    this.dictionarytype = result.data;
					})
					.catch(result => {
					})
			},
		},
		mounted() {
			this.getDictionaryDetails();
			this.getDictionaryType();
		}
	}

</script>

<style scoped>

</style>